//
//  CommissionSetCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/19.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CommissionSetCell.h"

@interface CommissionSetCell()

@property(nonatomic,copy)balanceStrikeBlock balanceStrikeBlock;
@property(nonatomic,copy)applyBlock applyBlock;

@property(nonatomic,strong)UITextField *strikeTextField;

@property(nonatomic,strong)UILabel *balanceLabel;

@end

@implementation CommissionSetCell

- (instancetype)initWithFrame:(CGRect)frame commissionBalance:(NSString *)commissionBalance strikeStr:(balanceStrikeBlock)balanceStrikeBlock applyClick:(applyBlock)applyBlock
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.balanceStrikeBlock = balanceStrikeBlock;
        self.applyBlock = applyBlock;
        
        [self setupViewCommissionBalance:commissionBalance];
    }
    return self;
}


- (void)setupViewCommissionBalance:(NSString *)balanceStr
{
    CGFloat leftMargin = 15;
    CGFloat topMargin = 10;
    
    __weak typeof(self) weakSelf = self;
    
    UILabel *topLabel = [[UILabel alloc] init];
    topLabel.text = @"申请结算金额";
    topLabel.textColor = [UIColor darkGrayColor];
    topLabel.font = FONT(15);
    [self addSubview:topLabel];
    
    UIImageView *iconImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_commission_money"]];
    [self addSubview:iconImg];
    iconImg.contentMode = UIViewContentModeCenter;
    
    
    //输入框
    [self addSubview:self.strikeTextField];
    @weakify(self);
    [self.strikeTextField.rac_textSignal subscribeNext:^(NSString *str) {
        
        @strongify(self);
        if ([str floatValue] > [balanceStr floatValue]) {
            self.balanceLabel.text = @"佣金金额已经超过结算金额";
            self.balanceLabel.textColor = DEFAULT_COLOR;
        }else if ([str floatValue] <= [balanceStr floatValue]){

            self.balanceLabel.text = [NSString stringWithFormat:@"佣金金额: %@ 元",balanceStr];
            self.balanceLabel.textColor = [UIColor grayColor];
        }
        
    }];

    
    if (self.balanceStrikeBlock) {
        weakSelf.balanceStrikeBlock(weakSelf.strikeTextField);
    }
    //闪屏
    if (balanceStr.length > 0) {
        [_strikeTextField becomeFirstResponder];
    }
    
    self.balanceLabel.text = [NSString stringWithFormat:@"佣金金额: %@ 元",balanceStr];
    self.balanceLabel.textColor = [UIColor grayColor];
    [self addSubview:self.balanceLabel];
    
    //分割线
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = [UIColor blackColor];
    lineView1.alpha = 0.1;
    [self addSubview:lineView1];
    
    
    
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top).offset(leftMargin);
        make.left.equalTo(weakSelf.mas_left).offset(leftMargin);
    }];
    
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLabel.mas_bottom).offset(topMargin*2+5);
        make.left.equalTo(topLabel.mas_left);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    
    [self.strikeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconImg.mas_centerY);
        make.left.equalTo(iconImg.mas_right).offset(topMargin);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(WIDTH-55);
    }];
    
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.strikeTextField.mas_bottom).offset(topMargin);
        make.width.mas_equalTo(WIDTH-leftMargin*2);
        make.left.equalTo(topLabel.mas_left);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topLabel.mas_left);
        make.top.equalTo(lineView1.mas_bottom).offset(topMargin);
        
    }];
    
}


- (UITextField *)strikeTextField
{
    if (!_strikeTextField) {
        _strikeTextField = [[UITextField alloc] init];
        _strikeTextField.textAlignment = NSTextAlignmentLeft;
        _strikeTextField.keyboardType = UIKeyboardTypeDecimalPad;
        _strikeTextField.clearButtonMode = UITextFieldViewModeAlways;
        _strikeTextField.font = FONT(45);
       
    }
    return _strikeTextField;
}

- (UILabel *)balanceLabel
{
    if (!_balanceLabel) {
        _balanceLabel = [[UILabel alloc] init];
        _balanceLabel.textAlignment = NSTextAlignmentLeft;
        _balanceLabel.font = FONT(13);
    }
    return _balanceLabel;
}

@end
